#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

import time

from Common.HbaseCommon import HbaseCommon
from Common.KafkaCommon import Kafka_consumer
from Common.configCommon import configCommon


def main():

    #先从kafka消费获取到数据
    consumer = Kafka_consumer(configCommon.getvales("Kafka","address"),
                              configCommon.getvales("Kafka","port"),
                              configCommon.getvales("Kafka","topic"),
                              configCommon.getvales("Kafka","groupid"))
    message = consumer.consume_data()
    for i in message:
        #将kafka值转换成json对象
        value=str(i.value,encoding="utf-8")
        jsoninfo = json.loads(value)
        print("INFO by kaFka info:")
        print(jsoninfo)
        #jsontxt = '{"name":"TOM1","age":181,"sex":2,"carId":421125199206260610}'
        #操作Hbase （需要  表明 列名  列对应的值）
        #rowKey (暂且用身份证Id)
        rowkey=str(jsoninfo["carId"])
        #列
        mutations=[]
        mutations.append(["cf:name",str(jsoninfo["name"])])
        mutations.append(["cf:age",str(jsoninfo["age"])])
        mutations.append(["cf:sex",str(jsoninfo["sex"])])
        mutations.append(["cf:carId",str(jsoninfo["carId"])])
        #执行插入操作
        result = HbaseCommon.addrow("TestBase",rowkey,mutations)
        print(result)
        print("Hbase Add Row Success!")


if __name__ == '__main__':
    main()

	
	
	
	
	